package main

import (
	"fmt"
	"time"
)

func service() string {
	time.Sleep(time.Millisecond * 500) //休息500毫秒
	return "处理完成"
}

func AsyncService() chan string {
	retCh := make(chan string, 1)
	go func() {
		ret := service()
		fmt.Println("returned result.")
		retCh <- ret
		fmt.Println("service exited.")
	}()
	return retCh
}

func main() {
	select {
	case ret := <-AsyncService():
		fmt.Println(ret)
	case <-time.After(time.Millisecond * 100): //设定超时时间为100毫秒
		fmt.Println("超时")
	}
}
